<?php
// +----------------------------------------------------------------------
// |     【ClothColor】Model模型类
// +----------------------------------------------------------------------

declare (strict_types = 1);

namespace app\common\model;

use think\Model;
use think\model\concern\SoftDelete;

class ClothColor extends Model{
    use SoftDelete;
    protected $deleteTime = false;
    // 获取列表
    public static function getList($where = []){
        $limit = I('get.limit/d');
		$pid = I('pid',0);
        
		//按隶属产品编号查找
		if ($number = I("number")) {
		   $where[] = ["number", "like", "%" . $number . "%"];
		}
		
		if ($pid>0) {
		   $where[]=['pid','=',$pid];
		}
		
        $list = self::order(['sort'=>'asc','colorid'=>'asc'])->where($where)->paginate($limit);
        return ['code'=>0,'data'=>$list->items(),'extend'=>['count' => $list->total(), 'limit' => $limit]];
    }
	
	// 获取列表数据
	public static function listData($where=[],$order="",$n_page=0,$limit=20){
		$data=self::order($order)->where($where);
		if($n_page>0){
			$data=$data->page($n_page,$limit);
		}
		$data=$data->select()->toArray();
		
		return $data;
	}
	
	// 获取某个单条记录
	public static function info($id){
		if(!is_array($id)){
			$model=self::find($id);
		}else{
			$model=self::where($id)->find();
		}
		return $model;		
	}
	
	// 批量修改颜色RGB
	public static function upRGB($where=[],$now_page=0,$limit=1000){
		$data=self::listData($where,"",$now_page,$limit);
		if($data){
			foreach($data as $v){
				$rgb=self::toRgb($v['val']);
				if($rgb[0]==$v['red'] && $rgb[1]==$v['green'] && $rgb[2]==$v['blue']){
					continue;
				}else{
					$n_data=[
						'red' => $rgb[0],
						'green' => $rgb[1],
						'blue' => $rgb[2],
					];
					self::where(['id'=>$v['id']])->save($n_data);
				}
			}
			return $now_page+1;	
		}else{
			return 0;
		}
	}
	
	//把16进制的颜色转化出RGB
	private static function toRgb($val){
		$data=[];
		if($val!=''){
			$rgb=hex2rgba('#'.$val);
			if(count($rgb)>1){
				$data=$rgb;
			}
		}
		if(count($data)<=0){
			$data=[0,0,0];
		}
		return $data;
	}
	
	//写入后
	public static function onAfterWrite($model){
		F($model->uuid.'/minApp/color_'.$model->pid,null);	//清除缓存
	}
	
	/** 获取器 **/
	
}
